﻿using System.Threading.Tasks;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace Acme.BookStore
{
    /// <summary>
    /// 工作流前中间件
    /// </summary>
    public class AddDescriptionWorkflowMiddleware : IWorkflowMiddleware
    {
        public WorkflowMiddlewarePhase Phase =>
            WorkflowMiddlewarePhase.PreWorkflow;

        public Task HandleAsync(
            WorkflowInstance workflow,
            WorkflowDelegate next
        )
        {
            if (workflow.Data is IDescriptiveWorkflowParams descriptiveParams)
            {
                workflow.Description = descriptiveParams.Description;
            }

            return next();
        }
    }

    // IDescriptiveWorkflowParams.cs
    public interface IDescriptiveWorkflowParams
    {
        string Description { get; }
    }

    // MyWorkflowParams.cs
    public class MyWorkflowParams : IDescriptiveWorkflowParams
    {
    public string Description => $"Run task '{TaskName}'";

    public string TaskName { get; set; }
  }
}
